//
//  BezierViewController.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/7/25.
//  Copyright © 2016年 吴可高. All rights reserved.
//
@interface BezierView : UIView

@end

@implementation BezierView
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

#pragma mark Initialize View Style
-(void)setUp
{
    self.backgroundColor = KColor(247, 247, 247);
}
-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef  context = UIGraphicsGetCurrentContext();
//    UIBezierPath  * bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 200) radius:80 startAngle:0 endAngle:2*M_PI clockwise:YES];
//    [bezierPath setLineWidth:10.f];
//    CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
//    CGContextSetLineWidth(context, 20.f);
//    CGContextSetShadowWithColor(context, CGSizeMake(0.0001, 0.001), 100.f, [UIColor grayColor].CGColor);
//    CGContextAddPath(context, bezierPath.CGPath);
//    CGContextStrokePath(context);
//    //
//    UIBezierPath * bezier = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 200) radius:70 startAngle:0 endAngle:2 * M_PI clockwise:YES];
//    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
//    CGContextAddPath(context, bezier.CGPath);
//    CGContextDrawPath(context, kCGPathFill);
    CGContextRef contextRef =UIGraphicsGetCurrentContext();
    CGContextAddArc(contextRef, 200, 200, 60, 0, 2 *M_PI, YES);
    CGContextSetStrokeColorWithColor(contextRef, [UIColor yellowColor].CGColor);
    CGContextSetShadowWithColor(contextRef, CGSizeMake(0.001f, 0.001f), 40.f, UIColorFromRGB(0xf8c301).CGColor);
    CGContextSetLineWidth(contextRef, 10.f);
    CGContextStrokePath(contextRef);
    //
    CGContextAddArc(contextRef, 200, 200, 55, 0,2*M_PI, YES);
    CGContextSetFillColorWithColor(contextRef, [UIColor whiteColor].CGColor);
    CGContextSetStrokeColorWithColor(contextRef, [UIColor whiteColor].CGColor);
    CGContextFillPath(contextRef);
    //
    CGMutablePathRef mutableRef =  CGPathCreateMutable();
    CGPathAddArc(mutableRef, &CGAffineTransformIdentity, 100, 100, 60, 0, 2* M_PI, YES);
    CGPathRef pathRef = CGPathCreateCopy(mutableRef);
    CGContextSetStrokeColorWithColor(contextRef, [UIColor redColor].CGColor);
    CGContextSetLineWidth(contextRef, 18.f);
    CGContextAddPath(contextRef, pathRef);
    CGContextDrawPath(contextRef, kCGPathStroke);
    //
    CGPathAddArc(mutableRef, &CGAffineTransformIdentity, 100, 100, 55, 0, 2* M_PI, YES);
    CGContextSetStrokeColorWithColor(contextRef, [UIColor whiteColor].CGColor);
    CGContextAddPath(contextRef, pathRef);
    CGContextDrawPath(contextRef, kCGPathFill);
   
}
#pragma mark View Algorithm Processor
#pragma mark Setter Method View Style
#pragma mark Getter Method View Style
#pragma mark IBOutlet Action View
#pragma mark Possess  Delegate View（委托代理者）
#pragma mark Other Method
-(void)layoutSubviews
{
    [super layoutSubviews];
}

@end

@interface IndicatorView : UIView
@end
@implementation IndicatorView
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
-(void)setUp
{
    self.backgroundColor = [UIColor whiteColor];
}
-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(10, 10)];
    [bezierPath addLineToPoint:CGPointMake(20, 38)];
    [bezierPath addLineToPoint:CGPointMake(49, 89)];
    [bezierPath addLineToPoint:CGPointMake(100, 45)];
    [bezierPath addLineToPoint:CGPointMake(200, 100)];
    [bezierPath addLineToPoint:CGPointMake(300, 150)];
    [bezierPath addLineToPoint:CGPointMake(310, 80)];
   
    
    CAShapeLayer * layer1 = [CAShapeLayer layer];
    layer1.frame = CGRectMake(0, 0,20, 20);
    UIImage * nextImg  = [UIImage imageNamed:@"next"];
    layer1.contents = (__bridge id)nextImg.CGImage;
    layer1.contentsScale = nextImg.scale;
    [layer1 setStrokeColor:[UIColor clearColor].CGColor];
    [layer1 setFillColor:[UIColor clearColor].CGColor];
    layer1.path =  bezierPath.CGPath;
    [self.layer addSublayer:layer1];
    //
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineWidth = 2.f;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeEnd = 0.f;
    shapeLayer.strokeColor = [UIColor blueColor].CGColor;
    shapeLayer.path = bezierPath.CGPath;
    [self.layer addSublayer:shapeLayer];
    
    CAKeyframeAnimation * keyframeAnimation  = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyframeAnimation.duration = 10.f;
    keyframeAnimation.path = bezierPath.CGPath;
    keyframeAnimation.autoreverses = NO;
    keyframeAnimation.removedOnCompletion =  NO;
    keyframeAnimation.fillMode =  kCAFillModeForwards;
    keyframeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [layer1 addAnimation:keyframeAnimation forKey:@"position"];
    
     CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];
     pathAnimation.duration = 10.f;
     pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
     pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
     pathAnimation.toValue = [NSNumber numberWithFloat:1.f];
     pathAnimation.autoreverses = NO;
     pathAnimation.removedOnCompletion = NO;
     pathAnimation.fillMode = kCAFillModeForwards;
     [shapeLayer addAnimation:pathAnimation forKey:NSStringFromSelector(@selector(strokeEnd))];

}

@end
#import "BezierViewController.h"

@interface BezierViewController ()
@property (strong, nonatomic) IndicatorView  * indicatorView;

@property (strong, nonatomic) BezierView * bezierView;

@end

@implementation BezierViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self  setObjectView];
}
#pragma mark ViewController AsynRequestData

#pragma mark ViewController Algorithm Processor

#pragma mark ViewController Initialize SubObjectView
-(void)setObjectView
{
    __weak  typeof(self)weakSelf = self;
    [self.view addSubview:self.indicatorView];
    [self.bezierView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(kNavigationBarHeight*2.f);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).with.offset(-kNavigationBarHeight);
    }];
}
#pragma mark --viewController getter methods
-(IndicatorView *)indicatorView
{
    _bezierView = ({
        if (!_indicatorView) {
            _indicatorView  = [[IndicatorView alloc]initWithFrame:CGRectZero];
        }
        _indicatorView;
    });
    return _indicatorView;
}
#pragma mark Possess  Delegate ViewController（委托代理者）
#pragma mark IBOutlet Action ViewController

#pragma mark Other Method
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
}

@end
